package com.taotao.portal.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.HttpClientUtil;
import com.taotao.common.utils.JsonUtils;
import com.taotao.pojo.TbUser;
import com.taotao.portal.service.UserService;

@Service
public class UserServiceImpl implements UserService {
	
	@Value("${SSO_BASE_URL}")
	private String SSO_BASE_URL;
	@Value("${SSO_LOGIN_PAGE}")
	private String SSO_LOGIN_PAGE;
	@Value("${SSO_GET_USER_BY_TOKEN}")
	private String SSO_GET_USER_BY_TOKEN;
	

	public String getSSOBaseUrl() {
		return SSO_BASE_URL;
	}
	
	public String getLoginPage(String token,String redirect) {
		

		String url=SSO_BASE_URL+SSO_LOGIN_PAGE;
		
		if(!StringUtils.isBlank(token))
			url+="/"+token;
		
		url+="?redirect="+redirect;
		
		return url;
	}

	
	
	@Override
	public TbUser getUserByToken(String token) throws Exception {
		if(StringUtils.isBlank(token))
			return null;
		String url=SSO_BASE_URL+SSO_GET_USER_BY_TOKEN+"/"+token;
		String json = HttpClientUtil.doGet(url);
		if(StringUtils.isBlank(token)) {
			throw new Exception("获取用户信息失败");
		}
		TaotaoResult result = TaotaoResult.formatToPojo(json, TbUser.class);
		if(result.getStatus()==200)
			return (TbUser) result.getData();
		return null;
	}

}
